home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 700 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. From: mtm4@rsvl.unisys.com (Michael McCormick)
  2. Message-ID: <Do81tp.H9u@rsvl.unisys.com>
  3. X-Original-Date: Wed, 13 Mar 1996 19:52:40 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 13 Mar 96 23:40:59 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Return-Path: <daemon@meeker.UCAR.EDU>
  8. Newsgroups: comp.std.c++
  9. Subject: Re: String value of enum
  10. References: <4i5sf3$89c@hermes.is.co.za>
  11. Organization: Unisys
  12. X-Newsreader: Forte Free Agent 1.0.82
  13. Content-Type: text
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBFAgUBMUddJ+EDnX0m9pzZAQEVVwF/XFA3mDqWpcigrbyPdU8aWaVW9ZewW9ER
  16.     d0oZMWIjQz84nDPu8WJJUpgqSg7+ScuD
  17.     =s0Hy
  18.  
  19. "W. Dicks" <wd@isis.co.za> shared the following on 13 Mar 96 07:31:13 GMT:
  20.  
  21. >The system that I'm working often needs to know the string 
  22. >value of an enum. e.g. enum week{MON=1, ..., SUN} it; it = 
  23. >TUE;
  24. >Then when this enum is passed as a parameter the function 
  25. >should return the string based on the enum. For instance, 
  26. >weekImage(it) will return "TUE". Is it not possible that such 
  27. >a functionality can be built into enums?
  28.  
  29. What you are essentially asking for is language support for converting
  30. a label into a string respresentation of its name.  That is a very
  31. unusual feature to find in any language, since it would require the
  32. compiler to place the symbolic dictionary in the executable file.  I
  33. don't think there's a snowball's chance of this getting into C++.
  34.  
  35. Since enum is by definition an integral type, why not assign values to your
  36. enumerators that can be used as indexes into a string?:
  37.  
  38.  enum week {MON=0,TUE=4,WED=8,THU=12,FRI=16,SAT=20,SUN=24};
  39.  const char * days = "MON\0TUE\0WED\0THU\0FRI\0SAT\0SUN";
  40.  
  41.  char const * weekImage(week day) = days[day];
  42.  
  43.  
  44.  
  45. ------------------------------------------------------------------
  46.  Mike McCormick // mtm4@rsvl.unisys.com // m.mccormick2@genie.com
  47. ------------------------------------------------------------------
  48.                    Emperor of Cubicle J240-46
  49. ------------------------------------------------------------------
  50.      All views expressed are my own, not those of my employer.
  51. ------------------------------------------------------------------
  52.  While you're out surfing the internet...
  53.  I'm back on the beach blowing my little lifeguard whistle.
  54. ------------------------------------------------------------------
  55. ---
  56. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  57. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  58. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  59. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  60. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  61.